package com.wugh.fast.module.api.vo;

import com.wugh.fast.kernel.core.enums.EnumInterface;
import com.wugh.fast.kernel.core.enums.FastExceptionEnum;
import com.wugh.fast.kernel.core.exception.FastExceptionMark;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 说明: 响应视图
 *
 * @author wugh
 * @date 2019-01-24 10:55
 */
@Data
@Accessors(chain = true)
@ApiModel("响应视图")
public class ResponseVO<T> {
    private static final String SUCCESS_OPERATE = "suc";
    private static final String FAIL_OPERATE = "fail";
    @ApiModelProperty("操作结果, suc:成功; fail:失败")
    private String op;
    @ApiModelProperty("响应编码 0: 成功; 其他:异常")
    private Integer code;
    @ApiModelProperty("错误信息")
    private String msg;
    @ApiModelProperty("返回的业务数据")
    private T data;

    public static <T> ResponseVO<T> ok(T data) {
        return new ResponseVO<T>()
                .setCode(FastExceptionEnum.SUCCESS.getCode())
                .setOp(SUCCESS_OPERATE)
                .setData(data);
    }

    public static ResponseVO<Void> ok() {
        return new ResponseVO<Void>()
                .setCode(FastExceptionEnum.SUCCESS.getCode())
                .setOp(SUCCESS_OPERATE)
                .setData(null);
    }

    public static ResponseVO<Void> fail(FastExceptionMark exception) {
        return new ResponseVO<Void>()
                .setCode(exception.getCode())
                .setOp(FAIL_OPERATE)
                .setData(null)
                .setMsg(exception.getMessage());
    }

    public static <T> ResponseVO<T> fail(T data, FastExceptionMark exception) {
        return new ResponseVO<T>()
                .setCode(exception.getCode())
                .setOp(FAIL_OPERATE)
                .setData(data)
                .setMsg(exception.getMessage());
    }
}
